package com.znxz.hzqiuxm.ziniublog.user.domain.services;

import com.znxz.hzqiuxm.ziniublog.global.common.SysConst;
import com.znxz.hzqiuxm.ziniublog.user.domain.entity.UserDO;
import com.znxz.hzqiuxm.ziniublog.user.domain.valobj.JwtUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Copyright © 2018年 ziniuxiaozhu. All rights reserved.
 *
 * @Author 临江仙 hzqiuxm@163.com
 * JwtUser工厂方法
 * @Date 2018/8/31 14:10
 */
public final class JwtUserFactory {

    private JwtUserFactory() {
    }

    /**
     * 组装验证所需的JWTUser对象
     * @param userDO 用户实体信息
     * @return JWTUser对象
     */
    public static JwtUser create(UserDO userDO){

        return new JwtUser(
                userDO.getId(),
                userDO.getUserName(),
                userDO.getShowName(),
                userDO.getPassword(),
                userDO.getEmail(),
                userDO.getLoginSecurity(),
                userDO.getRoles(),
                null);
    }

    /**
     * 权限格式转换
     * @param roles 用户角色，相当于用户权限 List<String></>
     * @return 权限列表
     */
    @Deprecated
    private static List<GrantedAuthority> mapToGrantedAuthorities(List<String> roles) {

        if(null!=roles && roles.size()>0){
            return roles.stream()
                    .map(SimpleGrantedAuthority::new)
                    .collect(Collectors.toList());
        }else {
            return SysConst.guestLists;
        }


    }


}
